package cn.tedu.home1ess.admin.content.service.impl;

import cn.tedu.common.exception.ServiceException;
import cn.tedu.common.response.StatusCode;
import cn.tedu.common.web.PageDataVO;
import cn.tedu.home1ess.admin.content.dao.repository.IVerificationDetailRepository;
import cn.tedu.home1ess.admin.content.pojo.param.PictureParam;
import cn.tedu.home1ess.admin.content.pojo.po.VerificationDetailPO;
import cn.tedu.home1ess.admin.content.pojo.vo.DetailPictureVO;
import cn.tedu.home1ess.admin.content.service.IVerificationDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class VerificationDetailServiceImpl implements IVerificationDetailService {

    @Value("${file-path}")
    private String dirPath;

    @Autowired
    private IVerificationDetailRepository verificationDetailRepository;
    @Override
    public PageDataVO<DetailPictureVO> listForPicture(Integer pageSize, Integer currentPage) {
        PageDataVO<VerificationDetailPO> orderPOs=verificationDetailRepository.pageOrderByUserAdminId(pageSize,currentPage);
        List<DetailPictureVO> pictureVOS=new ArrayList<>();
        //判断它是否为空
        if (orderPOs!=null&&!CollectionUtils.isEmpty(orderPOs.getList())){
            List<VerificationDetailPO> list = orderPOs.getList();
            for (VerificationDetailPO verificationDetailPO : list) {
                DetailPictureVO detailPictureVO = new DetailPictureVO();
                BeanUtils.copyProperties(verificationDetailPO,detailPictureVO);
                pictureVOS.add(detailPictureVO);
            }
        }else {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        PageDataVO<DetailPictureVO> result=new PageDataVO<>();
        BeanUtils.copyProperties(orderPOs,result);
        result.setList(pictureVOS);
        return result;
    }

    @Override
    public void addPictureById(PictureParam pictureParam) {
        verificationDetailRepository.addPicture(pictureParam.getId(),pictureParam.getHouseImg());

    }

    @Override
    public void deletePictureById(PictureParam pictureParam) {
        //删除磁盘中的文件信息
        new File(dirPath+pictureParam.getHouseImg()).delete();
        Long id = pictureParam.getId();
        String removeImg = pictureParam.getHouseImg();
        //根据id查询到我们需要的信息
        VerificationDetailPO po=verificationDetailRepository.selectPOById(id);
        //按照逗号分割为字符数组
        String[] pictureArray  = po.getHouseImg().split(",");
        //创建一个新的StringBuffer数组来保存不需要被删除的值
        StringBuffer stringBuffer = new StringBuffer();
        for (String s : pictureArray) {
            if (!s.equals(removeImg)){
                if (stringBuffer.length()>0){
                    stringBuffer.append(",");
                }
                stringBuffer.append(s);
            }
        }
        if (stringBuffer.length()==0){
            //最后一个数据需要将值置为null,但是不能直接用addPicture方法,因为他会识别为不需要更新东西
            verificationDetailRepository.deletePictureById(id);
        }else {
            //将图片路径进行更新
            verificationDetailRepository.addPicture(id,stringBuffer.toString());

        }

    }
}
